///////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2007 humbroll - WOC.  All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////////////////////////

registerNamespace("NcSoft.Openmaru.humbroll");

NcSoft.Openmaru.humbroll.GoButton = function(p_elSource, p_args, p_namespace) {
	NcSoft.Openmaru.humbroll.GoButton.initializeBase(this, arguments);
	
	var m_this = this;
	var m_id = p_args.id;
	
	function buttonClick(){
		m_this.fire("onclick",{id:m_id});
	}
	this.initialize = function(p_objScope){
        NcSoft.Openmaru.humbroll.GoButton.getBaseMethod(this, "initialize", "Web.Bindings.Base").call(this, p_objScope);
        p_elSource.attachEvent("onclick", buttonClick);
    };
    
    this.dispose = function(p_blnUnload){
        p_elSource.detachEvent("onclick", buttonClick);		
        NcSoft.Openmaru.humbroll.GoButton.getBaseMethod(this, "dispose", "Web.Bindings.Base").call(this, p_blnUnload);
    };
}
NcSoft.Openmaru.humbroll.GoButton.registerClass("NcSoft.Openmaru.humbroll.GoButton", "Web.Bindings.Base");
NcSoft.Openmaru.humbroll.GoButton.Events = Web.Enum.create("onclick");



NcSoft.Openmaru.humbroll.OpenBrain = function(p_elSource, p_args, p_namespace) {
    NcSoft.Openmaru.humbroll.OpenBrain.initializeBase(this, arguments);

	var M_EL = p_elSource;
	var m_this = this;
    var m_module = p_args.module;

	var m_gadgetmaindiv = null;
	var m_keyword = null;
	var m_search = null;
	var m_add = null;
	var m_home = null;

	var m_message = null;

	var m_resultlayout = null;
	var m_backwardspan = null;
	var m_backward = null;
	var m_update = null;
	var m_delete = null;
	var m_label = null;
	var m_resulttitle = null;
	var m_fieldset = null;
	var m_legend = null;
	var m_resultcontent = null;
	var m_idinresultlayout = null;
	var m_resultlistlayout = null;

	var m_updatelayout = null;
	var m_label_u = null;
	var m_updatetitle = null;
	var m_fieldset_u = null;
	var m_legend_u = null;
	var m_updatecontent = null;
	var m_updateok = null;
	var m_updatecancel = null;
	var m_idinupdatelayout = null;

	var m_addlayout = null;
	var m_label_a = null;
	var m_addtitle = null;
	var m_fieldset_a = null;
	var m_legend_a = null;
	var m_addcontent = null;
	var m_addok = null;
	var m_addcancel = null;

	var m_settingUserInfo = null;

	var m_setting = null;
	var m_setting_id_label = null;
	var m_setting_id = null;
	var m_setting_userkey_label = null;
	var m_setting_userkey = null;
	var m_setting_submit = null;
	var m_setting_cancel = null;

	var M_OPENID = "";
	var M_AJAXENGINE = null;
	var M_QUERY = null;
	var M_USERKEY = "";
	var M_PAGES = "";
	var M_BASEURL = "http://[URL]/pages/[detailURL]?format=xml";

	var M_MESSAGE_FAILED = "failed";
	var M_MESSAGE_LOAD = "loading...";

	var M_LOC_CREATEDATE = 0;
	var M_LOC_ID = 1;
	var M_LOC_NAME = 2;
	
//initialize Method----------------------------------------------------------------------
    // initialize is always called immediately after your object is instantiated
	this.initialize = function(p_objScope)
	{
	    NcSoft.Openmaru.humbroll.OpenBrain.getBaseMethod(this, "initialize", "Web.Bindings.Base").call(this, p_objScope);
		renderSetting();	
		settingUserInfo();
		setLayOut();
	};
	NcSoft.Openmaru.humbroll.OpenBrain.registerBaseMethod(this, "initialize");
	
	
//dispose Method----------------------------------------------------------------------
	this.dispose = function(p_blnUnload) {
		M_EL = null;
		m_module = p_args.module;

		m_gadgetmaindiv = null;
		m_keyword = null;
		m_search = null;
		m_add = null;
		m_home = null;

		m_message = null;

		m_resultlayout = null;
		m_backwardspan = null;
		m_backward = null;
		m_update = null;
		m_delete = null;
		m_label = null;
		m_resulttitle = null;
		m_fieldset = null;
		m_legend = null;
		m_resultcontent = null;
		m_idinresultlayout = null;
		m_resultlistlayout = null;

		m_updatelayout = null;
		m_label_u = null;
		m_updatetitle = null;
		m_fieldset_u = null;
		m_legend_u = null;
		m_updatecontent = null;
		m_updateok = null;
		m_updatecancel = null;
		m_idinupdatelayout = null;

		m_addlayout = null;
		m_label_a = null;
		m_addtitle = null;
		m_fieldset_a = null;
		m_legend_a = null;
		m_addcontent = null;
		m_addok = null;
		m_addcancel = null;
		m_settingUserInfo = null;

		m_settingUserInfo = null;

		m_setting = null;
		m_setting_id_label = null;
		m_setting_id = null;
		m_setting_userkey_label = null;
		m_setting_userkey = null;
		m_setting_submit = null;
		m_setting_cancel = null;
		
		M_OPENID = null;
		M_AJAXENGINE = null;
		M_QUERY = null;
		M_USERKEYNAME = null;
		M_USERKEY = null;
		M_PAGES = null;
		M_BASEURL = null;
		
		M_MESSAGE_FAILED = null;
		M_MESSAGE_LOAD = null;

		M_LOC_CREATEDATE = 0;
		M_LOC_ID = 1;
		M_LOC_NAME = 2;

		m_setting_submit.detachEvent("onclick",settingOk);
		m_setting_cancel.detachEvent("onclick",settingCancel);
		m_keyword.detachEvent("onkeypress",onTextKeyPress);
		m_search.detachEvent("onclick",onSearchClick);
		m_add.detachEvent("onclick",onAddClick);
		m_home.detachEvent("onclick",onHomeClick);
		m_backward.detachEvent("onclick",onBackwardClick);
		m_update.detachEvent("onclick",onUpdateClick);
		m_delete.detachEvent("onclick",onDeleteClick);
		m_updateok.detachEvent("onclick",onUpdateOkClick);
		m_updatecancel.detachEvent("onclick",onUpdateCancelClick);
		m_addok.detachEvent("onclick",onAddOkClick);
		m_addcancel.detachEvent("onclick",onAddCancelClick);
		m_settingUserInfo.detachEvent("onclick",reSettingUserInfo);
        // always call the base object's dispose last!	
		NcSoft.Openmaru.humbroll.OpenBrain.getBaseMethod(this, "dispose", "Web.Bindings.Base").call(this, p_blnUnload);
	};
	NcSoft.Openmaru.humbroll.OpenBrain.registerBaseMethod(this, "dispose");


		
//msin.js================================================================================
	function onOpen(){
		M_AJAXENGINE = new ajaxEngine();
		M_AJAXENGINE.createXHR();
		M_QUERY = new queryEngine();
	}
	function reSettingUserInfo(){
		settingShow();
	}
	function settingUserInfo(){
		//Attemp to retrieve saved preference
		M_OPENID = m_module.getPreference("M_ID");
		M_USERKEY = m_module.getPreference("M_USERKEY");
		if(M_OPENID == "" || M_OPENID == null ||  M_USERKEY =="" || M_USERKEY == null){
			m_module.setPreference("M_USERKEY",M_USERKEY);
			m_module.setPreference("M_ID",M_OPENID);
			settingShow();
		}else{
			m_setting_id.value = M_OPENID;
			m_setting_userkey.value = M_USERKEY;
			onOpen();
		}
	}
//rendering methods----------------------------------------------------------------------
	function renderSetting(){
			m_setting = document.createElement("div");
			m_setting.id = "settingdiv";
				m_setting_id_label = document.createElement("font");
				m_setting_id_label.innerText = "OpenID  : ";
			m_setting.appendChild(m_setting_id_label);
				m_setting_id = document.createElement("input");
				m_setting_id.type = "text";
				m_setting_id.onsize = "25";
				m_setting_id.id = "id";
			m_setting.appendChild(m_setting_id);
			m_setting.appendChild(document.createElement("br"));
				m_setting_userkey_label = document.createElement("font");
				m_setting_userkey_label.innerText = "UserKey : ";
			m_setting.appendChild(m_setting_userkey_label);
				m_setting_userkey = document.createElement("input");
				m_setting_userkey.type = "text";
				m_setting_userkey.onsize = "25";
				m_setting_userkey.id = "settingtext";
			m_setting.appendChild(m_setting_userkey);
				m_setting_submit = document.createElement("button");
				m_setting_submit.value="OK";
				m_setting_submit.attachEvent("onclick",settingOk);
				m_setting_submit.id = "settingbutton";
			m_setting.appendChild(m_setting_submit);
				m_setting_cancel = document.createElement("button");
				m_setting_cancel.attachEvent("onclick",settingCancel);
				m_setting_cancel.value="Cancel";
				m_setting_cancel.id= "settingbutton";
			m_setting.appendChild(m_setting_cancel);
		M_EL.appendChild(m_setting);
		M_EL.appendChild(document.createElement("br"));
		settingHide(); 
	}
	function setLayOut(){
			m_module.Resize();
			m_gadgetmaindiv = document.createElement("div");
			m_gadgetmaindiv.id = "gadgetmaindiv";
				m_keyword = document.createElement("input");
				m_keyword.type = "text";
				m_keyword.id = "keyword";
				m_keyword.name = "keyword";
				m_keyword.size = "14";
				m_keyword.attachEvent("onkeypress",onTextKeyPress);
			m_gadgetmaindiv.appendChild(m_keyword);
				m_search = document.createElement("button");
				m_search.id = "search";
				m_search.name = "search";
				m_search.innerHTML = "<b>GO</b>";
				m_search.attachEvent("onclick",onSearchClick);
			m_gadgetmaindiv.appendChild(m_search);
				m_add = document.createElement("button");
				m_add.id = "add";
				m_add.name = "add";
				m_add.innerHTML = "<b>ADD</b>";
				m_add.attachEvent("onclick",onAddClick);
			m_gadgetmaindiv.appendChild(m_add);
				m_home = document.createElement("button");
				m_home.id = "home";
				m_home.name = "home";
				m_home.innerHTML = "<b>HOME</b>";
				m_home.attachEvent("onclick",onHomeClick);
			m_gadgetmaindiv.appendChild(m_home);
		M_EL.appendChild(m_gadgetmaindiv);

			m_message = document.createElement("div");
			m_message.id = "message";
		M_EL.appendChild(m_message);

			m_resultlayout = document.createElement("div");
			m_resultlayout.id = "resultlayout";
			m_resultlayout.name = "resultlayout";
			m_resultlayout.style.display = "none";
				m_backwardspan = document.createElement("span");
					m_backward = document.createElement("button");
					m_backward.innerHTML = "<i><b>Backward</b></i>";
					m_backward.attachEvent("onclick",onBackwardClick);
				m_backwardspan.appendChild(m_backward);
			m_resultlayout.appendChild(m_backwardspan);
				m_update = document.createElement("button");
				m_update.id = "update";
				m_update.name = "update";
				m_update.innerHTML = "<b>UPDATE</b>";
				m_update.attachEvent("onclick",onUpdateClick);
			m_resultlayout.appendChild(m_update);
				m_delete = document.createElement("button");
				m_delete.id = "delete";
				m_delete.name = "delete";
				m_delete.innerHTML = "<b>DELETE</b>";
				m_delete.attachEvent("onclick",onDeleteClick);
			m_resultlayout.appendChild(m_delete);
			m_resultlayout.appendChild(document.createElement("br"));
				m_label = document.createElement("label");
				//m_label.for = "title";
				m_label.innerHTML = "<b><i>Title :</i></b>";
			m_resultlayout.appendChild(m_label);
				m_resulttitle = document.createElement("span");
				m_resulttitle.id = "resulttitle";
				m_resulttitle.name = "resulttitle";
				m_resulttitle.size = "20";
			m_resultlayout.appendChild(m_resulttitle);
				m_fieldset = document.createElement("fieldset");
					m_legend = document.createElement("legend");
					m_legend.innerHTML = "<b><i>Content</i></b>";
				m_fieldset.appendChild(m_legend);
					m_resultcontent = document.createElement("div");
					m_resultcontent.id = "resultcontent";
					m_resultcontent.rows = "10";
					m_resultcontent.cols = "30";
				m_fieldset.appendChild(m_resultcontent);
			m_resultlayout.appendChild(m_fieldset);
				m_idinresultlayout = document.createElement("input");
				m_idinresultlayout.type = "hidden";
			m_resultlayout.appendChild(m_idinresultlayout);	
		M_EL.appendChild(m_resultlayout);

			m_resultlistlayout = document.createElement("div");
			m_resultlistlayout.id = "resultlistlayout";
			m_resultlistlayout.style.display = "none";
		M_EL.appendChild(m_resultlistlayout);
			
			m_updatelayout = document.createElement("div");
			m_updatelayout.id = "updatelayout";
			m_updatelayout.name = "updatelayout";
			m_updatelayout.style.display = "none";
				m_label_u = document.createElement("label");
				m_label_u.innerHTML = "<b><i>Title :</i></b>";
			m_updatelayout.appendChild(m_label_u);
				m_updatetitle = document.createElement("input");
				m_updatetitle.type = "text";
				m_updatetitle.id = "updatetitle";
				m_updatetitle.size = "25";
			m_updatelayout.appendChild(m_updatetitle);
				m_fieldset_u = document.createElement("fieldset");
					m_legend_u = document.createElement("legend");
					m_legend_u.innerHTML = "<b><i>Content</i></b>";
				m_fieldset_u.appendChild(m_legend_u);
					m_updatecontent = document.createElement("textarea");
					m_updatecontent.rows = "10";
					m_updatecontent.cols = "45";
					m_updatecontent.id = "updatecontent";
					m_updatecontent.name = "updatecontent";
				m_fieldset_u.appendChild(m_updatecontent);
			m_updatelayout.appendChild(m_fieldset_u);
				m_updateok = document.createElement("button");
				m_updateok.id = "updateok";
				m_updateok.name = "updateok";
				m_updateok.attachEvent("onclick",onUpdateOkClick);
				m_updateok.innerHTML = "<b>OK</b>";
			m_updatelayout.appendChild(m_updateok);
				m_updatecancel = document.createElement("button");
				m_updatecancel.id = "updatecancel";
				m_updatecancel.name = "updatecancel";
				m_updatecancel.attachEvent("onclick",onUpdateCancelClick);
				m_updatecancel.innerHTML = "<b>UPDATE</b>";
			m_updatelayout.appendChild(m_updatecancel);
				m_idinupdatelayout = document.createElement("input");
				m_idinupdatelayout.id = "idinupdatelayout";
				m_idinupdatelayout.type = "hidden";
			m_updatelayout.appendChild(m_idinupdatelayout);
		M_EL.appendChild(m_updatelayout);

			m_addlayout = document.createElement("div");
			m_addlayout.id = "addlayout";
			m_addlayout.style.display = "none";
				m_label_a = document.createElement("label");
				m_label_a.innerHTML = "<b><i>Title :</i></b>";
			m_addlayout.appendChild(m_label_a);
				m_addtitle = document.createElement("input");
				m_addtitle.type = "text";
				m_addtitle.id = "addtitle";
				m_addtitle.name = "addtitle";
				m_addtitle.size = "40";
			m_addlayout.appendChild(m_addtitle);
				m_fieldset_a = document.createElement("fieldset");
					m_legend_a = document.createElement("legend");
					m_legend_a.innerHTML = "<b><i>Content</i></b>";
				m_fieldset_a.appendChild(m_legend_a);
					m_addcontent = document.createElement("textarea");
					m_addcontent.rows = "10";
					m_addcontent.cols = "38";
					m_addcontent.id = "addcontent";
					m_addcontent.name = "addcontent";
				m_fieldset_a.appendChild(m_addcontent);
			m_addlayout.appendChild(m_fieldset_a);
				m_addok = document.createElement("button");
				m_addok.id = "addok";
				m_addok.name = "addok";
				m_addok.innerHTML = "<b>OK</b>";
				m_addok.attachEvent("onclick",onAddOkClick);
			m_addlayout.appendChild(m_addok);
				m_addcancel = document.createElement("button");
				m_addcancel.id = "addcancel";
				m_addcancel.name = "addcancel";
				m_addcancel.innerHTML = "<b>Cancel</b>";
				m_addcancel.attachEvent("onclick",onAddCancelClick);
			m_addlayout.appendChild(m_addcancel);
		M_EL.appendChild(m_addlayout);

			m_settingUserInfo = document.createElement("button");
			m_settingUserInfo.id = "settingUserInfo";
			m_settingUserInfo.name = "settingUserInfo";
			m_settingUserInfo.innerHTML = "<i>Update UserInfo<i>";
			m_settingUserInfo.attachEvent("onclick",reSettingUserInfo);
		M_EL.appendChild(m_settingUserInfo);
	}
//---------------------------------------------------------------------------------------
//event methods--------------------------------------------------------------------------
	function onTextKeyPress(){
		if(event.keyCode == 27){
			m_keyword.value = "";
			return;
		}
		if(event.keyCode == 13){
			onSearchClick();
		}
	}
	function onSearchClick(){
		var m_keyWord = m_keyword.value;
		var m_query = M_QUERY.makeSearchQuery(m_keyWord.toLowerCase());
		if(m_keyWord == ""){
			alert("Insert Keyword~^^");
			return;
		}
		renderMessage(M_MESSAGE_LOAD,15);
		if(m_query == null){
			renderMessage(M_MESSAGE_FAILED,15);
			return;
		}
		//renderMessage(M_MESSAGE_LOAD,10);
		M_AJAXENGINE.openXHR("GET",m_query,handleSearchResult);
	}
	function onGoClick(e){
		var m_id = e.Package.id;
		M_AJAXENGINE.openXHR("GET",M_QUERY.makeGetPageQuery(m_id),makePageHtml);
		m_backwardspan.style.display = "block";
	}

	function onAddClick(){
		showAddLayout();
		m_module.Resize();
	}
	function onAddOkClick(){
		var m_title = m_addtitle.value;
		var m_content = m_addcontent.value;
		m_content = M_QUERY.htmlGenerator(m_content);
		var m_xml = M_QUERY.makeAddXMLPageQuery(m_title,m_content);
		var m_url = M_QUERY.makeAddPageQuery();
		M_AJAXENGINE.openXHR("POST",m_url,addResult,m_xml);
	}
	function onAddCancelClick(){
		m_addtitle.value = "";
		m_addcontent.value = "";
		showMessageLayout();
	}
	function onHomeClick(){
		try{
			window.open( M_OPENID,'new','left=300, top=300, width=900, height=700, resizable, directories, location, menubar, scrollbars, toolbar, status');
		}catch(e){
			alert(e.message);
	}
	}
	function onBackwardClick(){
		showResultListLayout();
	}
	function onUpdateClick(){
		showUpdateLayout();
	}
	function onUpdateOkClick(){
		if(confirm("Are you sure - UPDATE")){
			var m_id = m_idinupdatelayout.value;
			var m_url = M_QUERY.makeGetPageQuery(m_id);
			var m_title = m_updatetitle.value;
			var m_content = M_QUERY.htmlGenerator(m_updatecontent.value);
			var m_xml = M_QUERY.makeAddXMLPageQuery(m_title,m_content);
			M_AJAXENGINE.openXHR("PUT", m_url, updateResult, m_xml);
		}
	}
	function onUpdateCancelClick(){
		showResultLayout();
	}
	function onDeleteClick(){
		if(confirm("Are you sure - DELETE")){
			var m_id = m_idinresultlayout.value;
			M_AJAXENGINE.openXHR("DELETE", M_QUERY.makeGetPageQuery(m_id), deleteResult);
		}	
	}
	function settingOk(){
		if(m_setting_userkey.value == "" || m_setting_id.value == ""){
			alert("Setting UserInfo^^");
			return;
		}		
		m_module.setPreference("M_USERKEY", m_setting_userkey.value);
		m_module.setPreference("M_ID",m_setting_id.value);
		settingHide();
		settingUserInfo();
	}
	function settingCancel(){
		settingHide();
	}
//---------------------------------------------------------------------------------------
//callback methods-----------------------------------------------------------------------
	function handleSearchResult(p_result){
		var m_pages = p_result.getElementsByTagName("page");
		if(m_pages.length == 0){
			showMessageLayout();
			renderMessage(M_MESSAGE_FAILED,10);
			return;
		}
		for( var i = 0 ; i < m_pages.length ; i=i+1){
			if( (m_pages[i].childNodes[M_LOC_NAME].firstChild.nodeValue).toLowerCase().replace(/ /g,'') == (keyword.value).toLowerCase().replace(/ /g,'') ){
				var m_page_id = m_pages[i].childNodes[M_LOC_ID].firstChild.nodeValue;
				M_AJAXENGINE.openXHR("GET",M_QUERY.makeGetPageQuery(m_page_id),makePageHtml);
				m_backwardspan .style.display = "none";
				return;
			}
		}
		makePageListHtml(m_pages);
	}
	function makePageHtml(p_result){
		showResultLayout();
		var m_content = p_result.getElementsByTagName('source')[0].firstChild.nodeValue;
		var m_title = p_result.getElementsByTagName('title')[0].firstChild.nodeValue;
		var m_id = p_result.getElementsByTagName('identifier')[0].firstChild.nodeValue;
		m_resulttitle.innerHTML = m_title;
		m_resultcontent.innerHTML = m_content;
		m_idinresultlayout.value = m_id;
	}
	function makePageListHtml(p_pages){
		showResultListLayout();
		m_resultlistlayout.innerHTML = "";
		var m_contentlist = document.createElement("table");
		m_contentlist.style.width = "320px";
		m_contentlist.style.border = "1px solid #000";
		var m_div = document.createElement("div");
		var m_tr = null;
		var m_tdTitle = null;
		for(var i = 0 ; i < p_pages.length ; i=i+1){
			m_tr = document.createElement("tr");
			if(i%2 == 0){
				m_tr.className = "odd";
			}
			m_tdTitle = document.createElement("td");
			m_tdTitle.innerHTML = p_pages[i].childNodes[M_LOC_NAME].firstChild.nodeValue;
			m_tr.appendChild(m_tdTitle);
			m_tdGo = document.createElement("td");
			m_tdGo.style.width = "10px";
			m_tdGo.id = "bttd_" + i.toString();
			m_tr.appendChild(m_tdGo);
			m_contentlist.appendChild(m_tr);
		}
		m_div.appendChild(m_contentlist);
		m_resultlistlayout.innerHTML = m_div.innerHTML;
		for(var i = 0 ; i < p_pages.length ; i=i+1){
			var m_btGo = document.createElement("button");
			var m_tdId = "bttd_" + i;
			m_btGo.appendChild(document.createTextNode("GO"));
			(document.getElementById(m_tdId)).appendChild(m_btGo);
			
			var m_boundBtGo = Web.Bindings.attachElementBindingSync(
				m_btGo
				, NcSoft.Openmaru.humbroll.GoButton
				, m_this
				, {
					id : p_pages[i].childNodes[M_LOC_ID].firstChild.nodeValue
			});
			m_boundBtGo.attachEvent("onclick",onGoClick);
			m_btGo = null;
		}
	}
	function addResult(p_result){
		var m_errorMsg = checkError(p_result);
		if(m_errorMsg === "noerror"){
			makePageHtml(p_result);
			m_message.style.display = "block";
			m_message.innerHTML = "<font color=\"red\"><b><i>SUCCESS ADD~!</i></b></font>";
			m_backwardspan.style.display = "none";
		}else{
			alert(m_errorMsg);
			return;
		}
	}
	function updateResult(p_result){
		var m_errorMsg = checkError(p_result);
		if(m_errorMsg === "noerror"){
			makePageHtml(p_result);
			m_message.style.display = "block";
			m_message.innerHTML = "<font color=\"red\"><b><i>SUCCESS UPDATE~!</i></b></font>";
		}else{
			alert(m_errorMsg);
			return;
		}
	}
	function deleteResult(p_result){
		var m_errorMsg = checkError(p_result);
		if(m_errorMsg === "noerror"){
			makePageHtml(p_result);
			m_message .style.display = "block";
			message.innerHTML = "<font color=\"red\"><b><i>SUCCESS DELETE~!</i></b></font>";
		}else{
			alert(m_errorMsg);
			return;
		}
		setTimeout(showMessageLayout,3000);
	}
//error handling methods-----------------------------------------------------------------
	function checkError(p_xml){
		var m_page = p_xml.getElementsByTagName('page');
		if(m_page.length != 0){
			return "noerror";
		}
		else{
			var m_errorMsg = p_xml.getElementsByTagName('error');
			if(m_errorMsg.length > 0){
				m_errorMsg = m_errorMsg[0].firstChild.nodeValue;
				return m_errorMsg;
			}
			return "Failed~!";
		}
	}
	function renderMessage(p_string,p_size){
		showMessageLayout();
		if(p_size != -1){
			p_string = "<div style=\"font-family:tahoma;font-size:" + p_size + "px;\">" + p_string + "</div>";	
		}
		m_message.innerHTML = p_string;
	}
//---------------------------------------------------------------------------------------
//layout handler-------------------------------------------------------------------------
	function showMessageLayout(){
		m_message.style.display = "block";
		m_message.innerText = "";
		m_resultlistlayout .style.display = "none";
		m_resultlayout.style.display = "none";
		m_updatelayout.style.display = "none";
		m_addlayout.style.display = "none";
	}
	function showResultLayout(){
		m_message.style.display = "none";
		m_resultlistlayout.style.display = "none";
		m_resultlayout.style.display = "block";
		m_resulttitle.value = "";
		m_resultcontent.value = "";
		m_updatelayout.style.display = "none";
		m_addlayout.style.display = "none";
	}
	function showResultListLayout(){
		m_message.style.display = "none";
		m_resultlistlayout.style.display = "block";
		m_resultlayout.style.display = "none";
		m_updatelayout.style.display = "none";
		m_addlayout.style.display = "none";
	}
	function showUpdateLayout(){
		var m_title = m_resulttitle.innerText;
		var m_content = m_resultcontent.innerText;
		var m_id = m_idinresultlayout.value;
		m_message.style.display = "none";
		m_resultlistlayout.style.display = "none";
		m_resultlayout.style.display = "none";
		m_updatelayout.style.display = "block";
		m_addlayout.style.display = "none";
		m_updatetitle .value = m_title;
		m_updatecontent.value = m_content;
		m_idinupdatelayout.value = m_id;
	}
	function showAddLayout(){
		m_message.style.display = "none";
		m_resultlistlayout.style.display = "none";
		m_resultlayout.style.display = "none";
		m_updatelayout.style.display = "none";
		m_addlayout.style.display = "block";
		m_addtitle.value = "";
		m_addcontent.value = "";
	}
	function settingShow(){
		m_setting_id_label.style.display = "inline";
		m_setting_id.style.display = "inline";
		m_setting_userkey_label.style.display = "inline";
		m_setting_userkey.style.display = "inline";
		m_setting_submit.style.display = "inline";
		m_setting_cancel.style.display = "inline";
		m_setting.style.display = "inline";
	}
	function settingHide(){
		m_setting_id_label.style.display = "none";
		m_setting_id.style.display = "none";
		m_setting_userkey_label.style.display = "none";
		m_setting_userkey.style.display = "none";
		m_setting_submit.style.display = "none";
		m_setting_cancel.style.display = "none";
		m_setting.style.display = "none";
	}
//---------------------------------------------------------------------------------------


//ajaxEngine.js==========================================================================
var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADIN = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLETE = 4;

var SUCCESS_RECIEVE = 200;
var BAD_REQUEST = 400
var UNAUTHENTICATED = 401;
var NOT_FOUND = 404;
var INTERNAL_SERVER_ERROR = 500;

function ajaxEngine(){
	this.m_callBack = null;
	this.m_XHR = null;
}
ajaxEngine.prototype.createXHR = function(){
	try{
		this.m_XHR = new XMLHttpRequest();
	}catch(e){
		try{
			this.m_XHR = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			try{
				this.m_XHR = new ActiveXObejct("Msxml2.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
}
ajaxEngine.prototype.openXHR = function(p_method,p_url,p_callBack,p_content){
	this.m_callBack = p_callBack;
	if(p_method.toLowerCase() == "get"){
		this.openXHRGET(p_url,p_callBack);
	}else if(p_method.toLowerCase() == "post"){
		this.openXHRPOST(p_url,p_callBack,p_content);
	}else if(p_method.toLowerCase() == "put"){
		this.openXHRPUT(p_url,p_callBack,p_content);
	}else if(p_method.toLowerCase() == "delete"){
		this.openXHRDELETE(p_url,p_callBack);
	}
}
ajaxEngine.prototype.openXHRGET = function(p_url,p_callBack){
	try{
		this.m_XHR.open("GET",encodeURI(p_url),true);
		this.m_XHR.setRequestHeader("Authorization", getAuth(M_OPENID,M_USERKEY));
		
	}catch(e){
		alert("exception in XHR's open or setRequestHeader - GET");
		return;
	}
	var m_loader = this;
	this.m_XHR.onreadystatechange = function(){ m_loader.callBack(m_loader); };
	try{
		this.m_XHR.send(null);
	}catch(e){
		alert("exception in XHR's send - GET");
		return;
	}	
}
ajaxEngine.prototype.openXHRPOST = function(p_url,p_callBack,p_content){
	try{
		this.m_XHR.open("POST",encodeURI(p_url),true);
		this.m_XHR.setRequestHeader("Authorization", getAuth(M_OPENID,M_USERKEY));
	}catch(e){
		debug("exception in XHR's open or setRequestHeader - POST");
		return;
	}
	var m_loader = this;
	this.m_XHR.onreadystatechange = function(){ m_loader.callBack(m_loader); };
	try{
		this.m_XHR.send(p_content);
	}catch(e){
		debug("exception in XHR's send - POST");
		return;
	}
}
ajaxEngine.prototype.openXHRPUT = function(p_url,p_callBack,p_content){
	try{
		this.m_XHR.open("PUT",encodeURI(p_url),true);
		this.m_XHR.setRequestHeader("Authorization", getAuth(M_OPENID,M_USERKEY));
	}catch(e){
		alert("exception in XHR's open or setRequestHeader - PUT");
		return;
	}
	var m_loader = this;
	this.m_XHR.onreadystatechange = function(){ m_loader.callBack(m_loader); };
	try{
		this.m_XHR.send(p_content);
	}catch(e){
		alert("exception in XHR's send - PUT");
		return;
	}
}
ajaxEngine.prototype.openXHRDELETE = function(p_url,p_callBack){
	try{
		this.m_XHR.open("DELETE",encodeURI(p_url),true);
		this.m_XHR.setRequestHeader("Authorization", getAuth(M_OPENID,M_USERKEY));
	}catch(e){
		alert("exception in XHR's open or setRequestHeader - DELETE");
		return;
	}
	var m_loader = this;
	this.m_XHR.onreadystatechange = function(){ m_loader.callBack(m_loader); };
	try{
		this.m_XHR.send(null);
	}catch(e){
		alert("exception in XHR's send - DELETE");
		return;
	}	
}
ajaxEngine.prototype.callBack = function(p_loader){
	//alert("p_loader.m_XHR.readyState : "  + p_loader.m_XHR.readyState);
	if(p_loader.m_XHR.readyState != READY_STATE_COMPLETE){
		return;
	}
	//alert("p_loader.m_XHR.status : " + p_loader.m_XHR.status);
	if(p_loader.m_XHR.status != SUCCESS_RECIEVE){
		if(p_loader.m_XHR.status == BAD_REQUEST){
			alert("Reqeust Message is ivalid\mConfirm your request message.\n(xml/json)");
		}else if(p_loader.m_XHR.status == UNAUTHENTICATED){
			alert("Unauthenticated");
		}else if(p_loader.m_XHR.status == NOT_FOUND){
			alert("Not found.");
		}else if(p_loader.m_XHR.status == INTERNAL_SERVER_ERROR){
			alert("Internal server error");
		}
	}
	var result = p_loader.m_XHR.responseXML;
	p_loader.m_callBack(result);
}
//=======================================================================================
//queryEngine.js=========================================================================
var M_BASEURL = "http://api.springnote.com/pages[detailURL]";

function queryEngine(){
	this.m_baseURL = M_BASEURL;
}
queryEngine.prototype.makeSearchQuery = function(p_keyWord){
	var m_url = this.m_baseURL;
	m_url = m_url.replace('[detailURL]',".xml;search?q=" + p_keyWord);
	return m_url;
}
queryEngine.prototype.makeGetPageQuery = function(p_page_id){
	var m_url = this.m_baseURL;
	m_url = m_url.replace('[detailURL]',"/" + p_page_id + ".xml");
	return m_url;
}
queryEngine.prototype.makeAddPageQuery = function(){
	var m_url = this.m_baseURL;
	m_url = m_url.replace('[detailURL]',".xml");
	return m_url;
}
queryEngine.prototype.makeAddXMLPageQuery = function(p_title,p_content){
	var m_content_xml = "<?xml version='1.0' encoding='UTF-8'?>" 
						+ "<page>"
						+ "<source> [source] </source>"
						+ "<title> [title] </title>"
						+ "</page>";
	m_content_xml = m_content_xml.replace('[title]',p_title);
	p_content = this.htmlGenerator(p_content);
	m_content_xml = m_content_xml.replace('[source]',p_content);
	return m_content_xml;
}
queryEngine.prototype.htmlGenerator = function(p_string){
	p_string = p_string.replace(/\r\n/g,"&lt;br/&gt;");
	return p_string	
}
//deprecated---------------------------------------------------------
queryEngine.prototype.getIDbyKeyWord = function(p_keyWord){
	var pages = M_PAGES.getElementsByTagName("page");
	var m_name = "";
	for(var i = 1 ; i <  pages.length ; i++){
		m_name = pages[i].childNodes[1].firstChild.nodeValue;
		if(m_name == p_keyWord){
			return pages[i].childNodes[0].firstChild.nodeValue;
		}
	}
}
//-------------------------------------------------------------------
//=======================================================================================
//auth.js================================================================================
var devKey = "79fe1c017f513cc888aaaf3a697bfd4dbd014ce0";
var devId = "2";

function getAuth(p_id,p_userKey){
	var userKey = p_userKey;
    var nonce = generateNonce();
    var created = getCreated();
    var keydigest = SHA1(nonce + created + userKey + devKey);
    var password = encodeURIComponent(devId + "," + nonce + "," + created + "," + keydigest);
	var auth = "Basic " + Base64.encode(encodeURIComponent(p_id) + ":" + password);
	
	return auth
}
function generateNonce(){
   	var nonce = "";
   	for (var i = 0; i < 16; i++) {
		nonce += Math.floor(Math.random() * 16).toString(16);
   	}
   	return nonce;
}
function getCreated(){
	var m_d = new Date();
	var m_year = m_d.getUTCFullYear();
	var m_month = m_d.getUTCMonth() + 1;
	var m_date = m_d.getUTCDate();
	var m_hour = m_d.getUTCHours();
	var m_minute = m_d.getUTCMinutes();
	var m_second = m_d.getUTCSeconds();

	if(m_month  < 10) m_month = '0' + m_month;
	if(m_date   < 10) m_date = '0' + m_date;
	if(m_hour   < 10) m_hour = '0' + m_hour;
	if(m_minute < 10) m_minute = '0' + m_minute;
	if(m_second < 10) m_second = '0' + m_second;

	return  m_year + '-' + m_month + '-' + m_date + 'T' + m_hour + ':' + m_minute + ':' + m_second + 'Z';
}


/**
*
*  Secure Hash Algorithm (SHA1)
*  http://www.webtoolkit.info/
*
**/
function SHA1(msg) {

    function rotate_left(n,s) {
        var t4 = ( n<<s ) | (n>>>(32-s));
        return t4;
    };

    function lsb_hex(val) {
        var str="";
        var i;
        var vh;
        var vl;

        for( i=0; i<=6; i+=2 ) {
            vh = (val>>>(i*4+4))&0x0f;
            vl = (val>>>(i*4))&0x0f;
            str += vh.toString(16) + vl.toString(16);
        }
        return str;
    };

    function cvt_hex(val) {
        var str="";
        var i;
        var v;

        for( i=7; i>=0; i-- ) {
            v = (val>>>(i*4))&0x0f;
            str += v.toString(16);
        }
        return str;
    };


    function Utf8Encode(string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    };

    var blockstart;
    var i, j;
    var W = new Array(80);
    var H0 = 0x67452301;
    var H1 = 0xEFCDAB89;
    var H2 = 0x98BADCFE;
    var H3 = 0x10325476;
    var H4 = 0xC3D2E1F0;
    var A, B, C, D, E;
    var temp;

    msg = Utf8Encode(msg);

    var msg_len = msg.length;

    var word_array = new Array();
    for( i=0; i<msg_len-3; i+=4 ) {
        j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 |
        msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
        word_array.push( j );
    }

    switch( msg_len % 4 ) {
        case 0:
            i = 0x080000000;
        break;
        case 1:
            i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000;
        break;

        case 2:
            i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000;
        break;

        case 3:
            i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8    | 0x80;
        break;
    }

    word_array.push( i );

    while( (word_array.length % 16) != 14 ) word_array.push( 0 );

    word_array.push( msg_len>>>29 );
    word_array.push( (msg_len<<3)&0x0ffffffff );


    for ( blockstart=0; blockstart<word_array.length; blockstart+=16 ) {

        for( i=0; i<16; i++ ) W[i] = word_array[blockstart+i];
        for( i=16; i<=79; i++ ) W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);

        A = H0;
        B = H1;
        C = H2;
        D = H3;
        E = H4;

        for( i= 0; i<=19; i++ ) {
            temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B,30);
            B = A;
            A = temp;
        }

        for( i=20; i<=39; i++ ) {
            temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B,30);
            B = A;
            A = temp;
        }

        for( i=40; i<=59; i++ ) {
            temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B,30);
            B = A;
            A = temp;
        }

        for( i=60; i<=79; i++ ) {
            temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B,30);
            B = A;
            A = temp;
        }

        H0 = (H0 + A) & 0x0ffffffff;
        H1 = (H1 + B) & 0x0ffffffff;
        H2 = (H2 + C) & 0x0ffffffff;
        H3 = (H3 + D) & 0x0ffffffff;
        H4 = (H4 + E) & 0x0ffffffff;

    }

    var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);

    return temp.toLowerCase();

}

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
//=======================================================================================
};
NcSoft.Openmaru.humbroll.OpenBrain.registerClass("NcSoft.Openmaru.humbroll.OpenBrain", "Web.Bindings.Base");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("settingOk");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("settingCancel");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onTextKeyPress");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onSearchClick");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onAddClick");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onHomeClick");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onBackwardClick");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onUpdateClick");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onDeleteClick");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onUpdateOkClick");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onUpdateCancelClick");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onAddOkClick");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onAddCancelClick");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("reSettingUserInfo");
NcSoft.Openmaru.humbroll.OpenBrain.Events = Web.Enum.create("onGoClick");